/**
 * 
 */
package info.niwota.gae;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * @author qiangli
 *
 */
@SuppressWarnings("serial")
public class RedirectServlet extends BaseServlet {
	private String baseUrl;
	
	@Override
	public void init(ServletConfig config) throws ServletException {
		baseUrl = config.getInitParameter("baseUrl");
		baseUrl  = (baseUrl == null ? "" : baseUrl);
	}

	protected void sendRedirect(HttpServletRequest req, HttpServletResponse resp,
			String url) throws IOException {
		resp.sendRedirect(baseUrl + url);
	}
	
	@Override
	protected boolean handle(HttpServletRequest req, HttpServletResponse resp)
			throws Exception {
		String uri = req.getRequestURI();
		sendRedirect(req, resp, uri);
		return true;
	}
}
